<?php

namespace Home\Controller;

use Think\Controller;

/**
 * 学校类
 * @author 许杰
 * @phone 13675170078
 */
class SchoolController extends Controller {

    /**
     * 校园首页信息聚合
     */
    public function index() {
        $SchoolModel=D("School");
        $school['number'] = $this->school_number();
        //校园信息功能实现
        $this->schcool();
        $this->assign('ask_list', $SchoolModel->ask_show(I('get.school_id')));
        $this->assign('school', $school);

        $this->display();
    }

    /**
     * 校园信息功能实现
     */
    public function schcool() {
        $id = $id ? $id : I('get.school_id', 0);


        if ($id == 0) {
            $this->error('没有指定查询学校');
        }
        $School = D('School');
        $map['school_id'] = $id;
        //新增访问量
        $School->where($map)->setInc('visit');

        //查询当前学校相关属性
        $school_data = $School->relation(true)->where($map)->find();

        if ($school_data) {
            $SchoolRelation = M('SchoolRelation');
            $map_school_type['school_relation_id'] = $school_data['SchoolAddress']['province_relation_id'];
            $school_data['province'] = $SchoolRelation->where($map_school_type)->find();
            $map_school_type['school_relation_id'] = $school_data['SchoolAddress']['city_relation_id'];
            $school_data['city'] = $SchoolRelation->where($map_school_type)->find();

            $SchoolType = M('SchoolType');
            $map_type['school_type_id'] = $school_data['province']['school_type_id'];
            $school_data['province'] = $SchoolType->where($map_type)->getField('school_type_name');
            $map_type['school_type_id'] = $school_data['city']['school_type_id'];
            $school_data['city'] = $SchoolType->where($map_type)->getField('school_type_name');
        }


        //查询文章
        $DocumentModel=D("Document");
        $Document_data = $DocumentModel->documentlist($id,0,10,1,'`create_time` DESC');
        //查询高校详情
        $school_info = $this->info($map);
        //查询院系简介
        $departments = $this->departments($map);
        //查询高校图片
        $img_data = $this->imgs($map);
        //查询高校图片数量
        $img_num = $this->imgsnum($map);
        //查询评论
        $this->comment($map);
        //查询周边学校
        $this->around($school_data['SchoolRelation'], $map);
        //查询专业
        $this->professional($map);
        //查询学校地址
        $this->address($map);

        $this->assign('SchoolDepartments', $departments);
        $this->assign('school_info', $school_info);
        $this->assign('school_data', $school_data);
        $this->assign('Document', $Document_data);
        $this->assign('img_data', $img_data);
        $this->assign('img_num', $img_num);
//        dump($school_data['SchoolRelation']);
//        exit();
    }
    
    
    /**
     * 根据学校id获取高校图片
     * 按照类别排序 1 2 3 4
  	 * @param $map
     * @return unknown 
     * */
    public function imgs($map){
    	$img=M('SchoolImg');
    	$data=$img->where($map)->order('img_type')->limit(9)->select();
    	return $data;
    }
    /**
     * 根据学校id获取高校图片数量
  	 * @param $map
     * @return unknown 
     * */
    public function imgsnum($map){
        $img=M('SchoolImg');
        $data=$img->where($map)->count();
        return $data;
    }

    

    /**
     * 根据学校查询院系简介.
     * @param unknown_type $map
     * @return unknown
     */
    public function departments($map) {
        $departments = M('SchoolDepartments');
        $departments_data = $departments->where($map)->select();
        return $departments_data;
    }

    /**
     * 查询所有高招院校数量
     * @return unknown
     */
    public function school_number() {
        $SchoolNumber = M('SchoolRelation');
        $SchoolNumber_data = $SchoolNumber->where('school_type_id=1')->count();
        return $SchoolNumber_data;
    }

    /**
     * 查询该学校详情
     * @param unknown_type $map
     * @return unknown
     */
    public function info($map) {
        $SchoolInfo = M('SchoolInfo');
        $SchoolInfo_data = $SchoolInfo->where($map)->find();
        return $SchoolInfo_data;
    }

    /**
     * 查询该学校的所有专业
     * @param unknown_type $map
     */
    public function professional($map) {
        $professional = M('SchoolProfessional');
        $professional_data = $professional->where($map)->select();
        $this->assign('professional', $professional_data);
    }

    /**
     * 查询周边学校
     * @param unknown_type $around
     * @param unknown_type $wmap
     */
    public function around($around, $wmap) {

        foreach ($around as $v) {
            if ($v['school_type_sort'] == 3) {
                $map['school_type_id'] = $v['school_type_id'];
            }
        }
        $SchoolRelation = M('SchoolRelation');
        $map['school_id'] = array('neq', $wmap['school_id']);
        $SchoolRelation_data = $SchoolRelation->where($map)->getField('school_id', 6);
        $school_map['school_id'] = array('in', $SchoolRelation_data);
        $School = D('School');
        $School_data = $School->relation(true)->where($school_map)->select();
        $this->assign('around', $School_data);
    }

    /**
     * 查询评论(以及平均分)
     * @param unknown_type $map
     */
    public function comment($map) {
        $map['status']=0;
        $SchoolComment = M('SchoolComment');
        $SchoolComment_data = $SchoolComment->where($map)->select();
        $SchoolComment_avg = $SchoolComment->
                        field('	avg(teaching_int) as teaching,
					avg(teacher_int) as teacher,
					avg(facilities_int) as facilities,
					avg(surrounding_int) as surrounding,
					avg(traffic_int) as traffic')->
                        where($map)->find();


        $this->assign('comment_avg', $SchoolComment_avg);
        $this->assign('SchoolComment', $SchoolComment_data);
    }
    /**
     *查询学校地址
     * @param unknown_type $map   
     */
    public function address($map){
        $school_address=M("SchoolAddress");
        $address=$school_address->where($map)->order('main desc')->select();
        $this->assign('address', $address);
    }

    /**
     * 报名页面
     */
    public function apply() {


        if (IS_POST) { //登录验证
            /* 检测验证码 */
            if (!check_verify(I('post.verify'))) {
                $this->error('验证码输入错误！');
            }
            $SchoolApply = M('SchoolApply');
            if ($SchoolApply->create()) {
                if ($SchoolApply->add()) {

                    $this->success('提交成功,学校处理后会联系您', U('/School/index/school_id/' . I('post.school_id')));
                }
            }
        } else if (is_numeric(I('get.school_id'))) {
            $this->display();
        } else {
            $this->redirect('Home/Index');
        }
    }

    /**
     * 验证码
     */
    public function verify() {

        $verify = new \Think\Verify();
        //echo 123;
        ob_clean();
        $verify->entry(1);
    }

    /**
     * 添加评论
     */
    public function addComment() {


        $SchoolComment = M('SchoolComment');

        if ($SchoolComment->create()) {
            $a+=I('post.teaching_int') * 2;
            $a+=I('post.teacher_int') * 2;
            $a+=I('post.facilities_int') * 2;
            $a+=I('post.surrounding_int') * 2;
            $a+=I('post.traffic_int') * 2;
            $sum_avg = $a / 5;
            $SchoolComment->sum = $sum_avg;
            $SchoolComment->create_time = time();
            $SchoolComment->add();
            $avgScore = $SchoolComment->avg('sum');
            $school = M('school');
            $data['school_ratings']=$avgScore/2;
            $data['score'] = array('exp','comment+1');
            $school->where('school_id=' . I('post.school_id'))->save($data);
        }
        $this->redirect('School/index', array('school_id' => I('post.school_id')));
    }

    /*
     * 添加问题 
     *          */

    public function ask() {
        if ($_SESSION['onethink_home']['user_id']) {
            if ($_POST) {
               
                
                
                /* 检测验证码 */
            if (!check_verify(I('post.ask_yzm'))) {
                $this->ajaxReturn(array("status"=>"0","info"=>"验证码错误"));
                die();
            }
                
                $Model = M('SchoolAsk');

                $data['user_id'] = $_SESSION['onethink_home']['user_id'];
                $data['user_name'] = $_SESSION['onethink_home']['user_name'];
                $data['school_id'] = $_SESSION['onethink_home']['school_id'];
                $data['grade'] = $_POST['grade'];
                $data['title'] = $_POST['content'];
                $data['timeadd'] = time();

                //                $this->redirect('School/index');
                if ($Model->data($data)->add() > 0) {
                    $this->ajaxReturn(array("status"=>"1","info"=>"提问成功请耐心等待"));
//                    $this->success('提问成功请耐心等待', U('/School/index/school_id/' . $_SESSION['onethink_home']['school_id']));
                } else {
                    $this->ajaxReturn(array("status"=>"0","info"=>"提问失败请重新提问"));
//                    $this->error('提问失败请重新提问', U('/School/index/school_id/' . $_SESSION['onethink_home']['school_id']));
                }
            }
        } else {
             $this->ajaxReturn(array("status"=>"0","info"=>"您还未登录，不能发表问题"));
            //$this->error('您还未登录，不能发表问题');
        }
    }

    
    
    
    /**
     * 根据图片获取学校图片列表
     * 
     * */
    public function getImgs(){
    	$get_data=I('get.');
    	if($get_data['school_id']){
    		$map['school_id']=$get_data['school_id'];
    		$img=M('SchoolImg');
    		$data=$img->where($map)->order('upload_time desc')->select();
    	}
    	$this->ajaxReturn($data);
    }
    
    

}
